<!DOCTYPE html>
<html lang="en"
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
    <meta charset="UTF-8">
    <title>数字图书管理系统</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
    <style>
        nav {
            font-size: 1.5rem;
            background-color: #21D4FD;
            background-image: -webkit-linear-gradient(19deg, #21D4FD 0%, #B721FF 100%);
            background-image: -moz-linear-gradient(19deg, #21D4FD 0%, #B721FF 100%);
            background-image: -o-linear-gradient(19deg, #21D4FD 0%, #B721FF 100%);
            background-image: linear-gradient(19deg, #21D4FD 0%, #B721FF 100%);
        }
        h1 {
            font-size: 2rem;
            color: white;
        }
        li {
            margin: 10px 20px;
        }
        th {
            width: 100px;
        }
        label {
            margin: auto;
            padding-right: 20px;
        }
        .input-group {
            margin-bottom: 10px;
        }
    </style>
</head>
<body>
<nav class="navbar navbar-expand-sm bg-primary navbar-dark">
    <ul class="navbar-nav" sec:authorize="hasRole('ADMIN')">
        <li>
            <h1><a th:href="@{/}" style="color: white; text-decoration: none">数字图书管理系统</a></h1>
        </li>
        <li class="nav-item">
            <a class="nav-link" th:href="@{/admin/book/getAll}">图书管理</a>
        </li>
        <li class="nav-item">
            <a class="nav-link" th:href="@{/admin/reader/getAll}">读者管理</a>
        </li>
        <li class="nav-item">
            <a class="nav-link" th:href="@{/admin/record/getAll}">借还管理</a>
        </li>
    </ul>

    <ul class="navbar-nav" sec:authorize="hasRole('USER')">
        <li>
            <h1><a th:href="@{/}" style="color: white; text-decoration: none">数字图书管理系统</a></h1>
        </li>
        <li class="nav-item">
            <a class="nav-link" th:href="@{/user/book/getAll}">图书信息</a>
        </li>
        <li class="nav-item">
            <a class="nav-link" th:href="@{/user/record/getOwnRecord}">借阅记录</a>
        </li>
    </ul>
    <div class="text-white" style="font-size: medium; position: absolute; bottom: 30px; right: 40px">
        <div class="dropdown">
            <a class="dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"
               th:text="${session.name}+', 您好'">
            </a>
            <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
                <a class="dropdown-item" th:href="@{/user/toInfoPage}" sec:authorize="hasRole('USER')">个人信息</a>
                <a class="dropdown-item" th:href="@{/toChPwdPage}">修改密码</a>
                <a class="dropdown-item" th:href="@{/logout}">注销</a>
            </div>
        </div>
    </div>
</nav>

<div class="container">
    <form th:action="@{/changePassword}" method="post" style="margin-top: 40px" class="form">
        <div class="input-group">
            <label for="oldPassword">旧密码</label>
            <input type="text" class="form-control" id="oldPassword" name="oldPassword">
            <span class="invalid-feedback" id="errorPwd" style="display: none">
                密码错误！
            </span>
        </div>
        <div class="input-group">
            <label for="newPassword">新密码</label>
            <input type="text" class="form-control" id="newPassword" name="newPassword">
            <span class="invalid-feedback" id="emptyPwd" style="display: none">
                密码不能为空！
            </span>
        </div>
        <div class="input-group">
            <label for="checkPassword">确认密码</label>
            <input type="text" class="form-control" id="checkPassword" name="checkPassword">
            <span class="invalid-feedback" id="errorChecked" style="display: none">
                两次输入密码不一致！
            </span>
        </div>
        <input type="submit" value="修    改" class="btn btn-outline-primary btn-lg" style="margin-top: 20px">
        <a th:href="@{/}" class="btn btn-outline-secondary btn-lg" style="margin-left: 40px; margin-top: 20px">返&nbsp;&nbsp;&nbsp;&nbsp;回</a>
    </form>
</div>

<script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script>
    $(function () {
        $("#oldPassword").blur(
            function () {
                $.post("/checkPassword",
                    {
                        password: $(this).val()
                    },
                    function (data) {
                        if (data == "0") {
                            $("#oldPassword").removeClass("is-valid");
                            $("#oldPassword").addClass("is-invalid");
                            $("#errorPwd").show();
                        } else {
                            $("#oldPassword").removeClass("is-invalid");
                            $("#oldPassword").addClass("is-valid");
                            $("#errorMsg").hide();
                        }
                    })
            }
        );
        $("#newPassword").blur(function () {
            if ($("#newPassword").val() == '') {
                $("#newPassword").removeClass("is-valid");
                $("#newPassword").addClass("is-invalid");
                $("#emptyPwd").show();
            } else {
                $("#newPassword").removeClass("is-invalid");
                $("#newPassword").addClass("is-valid");
                $("#emptyPwd").hide();
            }
            if ($("#checkPassword").val() != $("#newPassword").val()) {
                    $("#checkPassword").removeClass("is-valid");
                    $("#checkPassword").addClass("is-invalid");
                    $("#errorChecked").show();
            } else {
                if ($("#checkPassword").val() == '') {
                    $("#checkPassword").removeClass("is-valid");
                    $("#checkPassword").removeClass("is-invalid");
                    $("#errorChecked").hide();
                } else {
                    $("#checkPassword").removeClass("is-invalid");
                    $("#checkPassword").addClass("is-valid");
                    $("#errorChecked").hide();
                }
            }
        });
        $("#checkPassword").blur(function () {
            if ($("#checkPassword").val() != $("#newPassword").val()) {
                $("#checkPassword").removeClass("is-valid");
                $("#checkPassword").addClass("is-invalid");
                $("#errorChecked").show();
            } else {
                if ($("#checkPassword").val() == '') {
                    $("#checkPassword").removeClass("is-valid");
                    $("#checkPassword").removeClass("is-invalid");
                    $("#errorChecked").hide();
                } else {
                    $("#checkPassword").removeClass("is-invalid");
                    $("#checkPassword").addClass("is-valid");
                    $("#errorChecked").hide();
                }
            }
        })
    });
</script>
</body>
</html>